package com.ruoyi.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.customer.service.ITMesCustomerService;
import com.ruoyi.inventory.domain.ProductCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.inventory.mapper.ProductMapper;
import com.ruoyi.inventory.domain.Product;
import com.ruoyi.inventory.service.IProductService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品Service业务层处理
 *
 * @author xwj
 * @date 2024-06-11
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ITMesCustomerService tMesCustomerService;

    /**
     * 查询商品
     *
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public Product selectProductById(Long id) {
        return productMapper.selectProductById(id);
    }

    /**
     * 查询商品列表
     *
     * @param product 商品
     * @return 商品
     */
    @Override
    public List<Product> selectProductList(Product product) {
        return productMapper.selectProductList(product);
    }

    /**
     * 新增商品
     *
     * @param product 商品
     * @return 结果
     */
    @Override
    public Product insertProduct(Product product) {

        //  productMapper.insertProduct(product);
        save(product);
        return product;
    }

    /**
     * 修改商品
     *
     * @param product 商品
     * @return 结果
     */
    @Override
    public Product updateProduct(Product product) {

        //  productMapper.updateProduct(product);
        updateById(product);
        return product;
    }

    /**
     * 批量删除商品
     *
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteProductByIds(Long[] ids) {
        return productMapper.deleteProductByIds(ids);
    }

    /**
     * 条件删除
     * @param product 商品
     * @return
     */
    @Override
    public int delete(Product product) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        QueryTool.condition(qw, product);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     * @param product 商品
     * @return
     */
    @Override
    public Pages<Product> queryPage(Product product) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        QueryTool.condition(qw, product);
        IPage<Product> ipage = new Query<Product>().getPage(product);

        IPage<Product> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    @Override
    public void setCategoryId(ProductCategory productCategory) {
        productMapper.setCategoryId(productCategory);
    }

    @Override
    public void updatePriceById(Long productId, Double amount) {
        Product product = getById(productId);
        product.setBuyPrice(amount);

        updateProduct(product);
    }

    @Override
    @Transactional
    public void batchSave(List<Product> productList, SysUser user) {
        Date now = new Date();
        for(Product product: productList) {
            String name = product.getName();
            Product exist = selectOne("name", name);
            product.setType(2);

            if(exist != null) {
                product.setId(exist.getId());
                product.setUpdateTime(now);
                product.setUpdateBy(user.getNickName());
                updateProduct(product);
            } else {
                String no = tMesCustomerService.generNo2("SP", "", "t_product");
                product.setNo(no);
                product.setCreateTime(now);
                product.setCreateBy(user.getNickName());

                insertProduct(product);
            }
        }
    }

    /**
     * 删除商品信息
     *
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int deleteProductById(Long id) {
        return productMapper.deleteProductById(id);
    }
}
